package com.example.common.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.example.common.MyApplication;

import java.util.HashMap;
import java.util.Map;

public class SPUtils {

    private static SPUtils sInstance;
    private SharedPreferences mSharedPreferences;

    public static final String UID = "uid";
    public static final String TOKEN = "token";
    public static final String USER_INFO = "userInfo";
    public static final String CONFIG = "config";
    public static final String IM_LOGIN = "imLogin";
    public static final String IM_LOGIN_STATUS = "imLoginStatus";
    public static final String IM_PHOTO = "imPhoto";
    public static final String XIAOMI_PUSH_REGID = "xiaomiPushRegid";

    private SPUtils() {
        mSharedPreferences = MyApplication.sInstener.getSharedPreferences("SharedPreferences", Context.MODE_PRIVATE);
    }

    public static SPUtils getInstance() {
        if (sInstance == null) {
            synchronized (SPUtils.class) {
                if (sInstance == null) {
                    sInstance = new SPUtils();
                }
            }
        }
        return sInstance;
    }

    public void setStringValue(String key, String value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public void setIntegerValue(String key, int value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public void setBooleanValue(String key, boolean value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public void setMultiStringValue(Map<String, String> data) {
        if (data == null || data.size() == 0) {
            return;
        }
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        for (Map.Entry<String, String> entry : data.entrySet()) {
            editor.putString(entry.getKey(), entry.getValue());
        }
        editor.apply();
    }

    public String getStringValue(String key) {
        return mSharedPreferences.getString(key, "");
    }

    public int getIntegerValue(String key) {
        return mSharedPreferences.getInt(key, 0);
    }

    public Map<String,String> getMultiStringValue(String...keys) {
        if (keys == null || keys.length == 0) {
            return null;
        }
        Map<String, String> data = new HashMap<>();
        for (String key : keys) {
            if (!TextUtils.isEmpty(key)) {
                data.put(key, mSharedPreferences.getString(key, ""));
            }
        }
        return data;
    }

    public void removeValue(String... keys) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        for (String key:keys) {
            editor.remove(key);
        }
        editor.apply();
    }

    public void removeAll() {
        String[] keys = {UID, TOKEN, USER_INFO, CONFIG, IM_LOGIN};
        removeValue(keys);
    }

}
